package com.project.web.wechat.controller.app.cpWeChat;

import com.project.common.core.utils.exception.Result;
import com.project.common.core.utils.file.FilePathConsts;
import com.project.common.core.utils.file.FileUtil;
import com.project.web.config.WxCorp.WxCpConfig;
import com.project.web.config.WxCorp.WxCpConfiguration;
import com.project.web.util.exception.WeChatCustomException;
import com.project.web.wechat.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMediaService;
import me.chanjar.weixin.cp.api.WxCpService;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * 素材管理接口
 *
 * @author wyy
 * @date 2020-06-24
 */
@Api(tags = "素材管理接口")
@RestController("workWxMediaController")
@RequestMapping("/weChat/auth/workWx/media")
public class WorkWxMediaController extends BaseController {

    /**
     * 上传临时素材
     *
     * @param media     媒体文件
     * @param mediaType 媒体类型
     * @param request   请求对象
     * @return
     */
    @ApiOperation("上传临时素材")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "media", value = "媒体文件", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(paramType = "query", name = "mediaType", value = "媒体类型", required = true, dataType = "String")
    })
    @PostMapping(value = "/upload")
    public Result<WxMediaUploadResult> upload(MultipartFile media, String mediaType, HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpMediaService mediaService = wxCpService.getMediaService();
        try {
            WxMediaUploadResult upload = mediaService.upload(mediaType, (File) media);
            return getResult(upload);
        } catch (WxErrorException e) {
            log.error("上传临时素材出错：{}", ExceptionUtils.getStackTrace(e));
            throw new WeChatCustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 获取临时素材
     *
     * @param mediaId 媒体ID
     * @param request 请求对象
     * @return
     */
    @ApiOperation("获取临时素材")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mediaId", value = "媒体文件", required = true, dataType = "MultipartFile"),
    })
    @PostMapping(value = "/download")
    public Result<String> download(String mediaId, HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpMediaService mediaService = wxCpService.getMediaService();
        try {
            File file = mediaService.download(mediaId);
            String nfsUrl = FileUtil.getNFSUrl((MultipartFile) file, FilePathConsts.FILE_MEDIA_PATH, Integer.valueOf(0));
            return getResult(nfsUrl);
        } catch (WxErrorException e) {
            log.error("获取临时素材文件出错：{}", ExceptionUtils.getStackTrace(e));
            throw new WeChatCustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 上传永久图片
     *
     * @param media   媒体文件
     * @param request 请求对象
     * @return
     */
    @ApiOperation("上传永久图片")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "media", value = "媒体文件", required = true, dataType = "MultipartFile")
    })
    @PostMapping(value = "/uploadImg")
    public Result<String> uploadImg(MultipartFile media, HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpMediaService mediaService = wxCpService.getMediaService();
        try {
            String s = mediaService.uploadImg((File) media);
            return getResult(s);
        } catch (WxErrorException e) {
            log.error("上传永久素材出错：{}", ExceptionUtils.getStackTrace(e));
            throw new WeChatCustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

    /**
     * 获取高清语音素材
     *
     * @param mediaId 媒体ID
     * @param request 请求对象
     * @return
     */
    @ApiOperation("获取高清语音素材")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mediaId", value = "媒体文件", required = true, dataType = "MultipartFile"),
    })
    @PostMapping(value = "/getJssdkFile")
    public Result<String> getJssdkFile(String mediaId, HttpServletRequest request) {
        // 企业微信基本参数
        WxCpConfig wxCpConfig = getWxCpConfig(request);
        WxCpService wxCpService = WxCpConfiguration.getCpService(wxCpConfig.getCorpId(), wxCpConfig.getAgentId());
        WxCpMediaService mediaService = wxCpService.getMediaService();
        try {
            File file = mediaService.getJssdkFile(mediaId);
            String nfsUrl = FileUtil.getNFSUrl((MultipartFile) file, FilePathConsts.FILE_MEDIA_PATH, Integer.valueOf(0));
            return getResult(nfsUrl);
        } catch (WxErrorException e) {
            log.error("获取高清语音素材出错：{}", ExceptionUtils.getStackTrace(e));
            throw new WeChatCustomException(String.valueOf(e.getError().getErrorCode()), e.getError().getErrorMsg());
        }
    }

}
